package com.kitbox.hl.meshow.framenwork.listener.lmpl;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.kitbox.hl.meshow.dao.ListenerMapper;
import com.kitbox.hl.meshow.framenwork.listener.ListenerService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.DelegateTask;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Modifier;

/**
 * @Auther: honglei
 * @Date: 2019/8/15 08:37
 * @Description:
 */
//@Service
public class ListenerServiceImpl implements ListenerService {
    private final Logger logger = LoggerFactory.getLogger(ExecutionListener.class);
    @Autowired
    private ListenerMapper listenerMapper;
//    @Autowired
//    private Gson gson ;
        Gson gson = new GsonBuilder()
        // 排除带有以下修饰符的 属性 -- 会 使 transient 失效
        .excludeFieldsWithModifiers(Modifier.TRANSIENT,Modifier.FINAL,
                Modifier.STATIC, Modifier.PRIVATE)
        .create();

    @Override
    public void notify(DelegateExecution delegateExecution) throws Exception {
        logger.info("任务监听器：");
        try {
            MyDelegateExecution myDelegateExecution=new MyDelegateExecution();
            MyCoreModelElement modelElement=new MyCoreModelElement();
            BeanUtils.copyProperties(delegateExecution,myDelegateExecution);
            myDelegateExecution.setEventSource(null);
            myDelegateExecution.setActivityName(delegateExecution.getCurrentActivityName());

            String json = gson.toJson(myDelegateExecution);
            listenerMapper.insert(1,json);

            logger.info(json);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void notify(DelegateTask delegateTask) {
        logger.info("任务监听器：");
        try {
            String json=gson.toJson(delegateTask);
            listenerMapper.insert(2,json);
            logger.info(json);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
